package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.AntiepidemicPurcMapper;
import com.suyuan.dao.VaccineInforMapper;
import com.suyuan.entity.*;
import com.suyuan.service.VaccInStoreService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

@Service(value = "vaccInStoreService")
public class VaccInStoreServiceImpl implements VaccInStoreService {
    @Resource
    private AntiepidemicPurcMapper antiepidemicPurcMapper;

    @Resource
    private VaccineInforMapper vaccineInforMapper;

    public PageInfo inquiry(Integer vaccineid, Date receivedate, String remark, Page page) {
        AntiepidemicPurcExample antiepidemicPurcExample=new AntiepidemicPurcExample();
        AntiepidemicPurcExample.Criteria criteria=antiepidemicPurcExample.createCriteria();
        if(vaccineid!=null){
           criteria.andVaccineidEqualTo(vaccineid);
        }
        if(receivedate!=null){
            criteria.andReceivedateEqualTo(receivedate);
        }
        if(remark!=null){
            criteria.andRemarkLike("%"+remark+"%");
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<AntiepidemicPurc> antiepidemicPurcs=antiepidemicPurcMapper.selectByExample(antiepidemicPurcExample);
        PageInfo info = new PageInfo(antiepidemicPurcs);
        return info;
    }

    /**
     * 修改疫苗入库信息
     * @param antiepidemicPurc 疫苗入库信息
     * @param oldNumInStore 原来的入库数量
     * @return 1成功 0失败
     */
    public int save(AntiepidemicPurc antiepidemicPurc,int oldNumInStore){
        int a=antiepidemicPurcMapper.insert(antiepidemicPurc);
        if(a==0){
            return a;
        }
        int vaccineid=antiepidemicPurc.getVaccineid();
        //若插入疫苗入库信息表没出错，则修改疫苗信息表的库存量
        /*//这种方法会把其他没设置的字段都set为null,不用
        VaccineInfor vaccineInfor=new VaccineInfor();
        vaccineInfor.setVaccineid(antiepidemicPurc.getVaccineid());
        vaccineInfor.setNuminstore(oldNumInStore+antiepidemicPurc.getAmount());
        vaccineInforMapper.updateByPrimaryKey(vaccineInfor);*/
        /*//这种也不行，疫苗信息的其他字段同样会set为null
        VaccineInfor vaccineInfor=vaccineInforMapper.selectByPrimaryKey(antiepidemicPurc.getVaccineid());
        vaccineInfor.setNuminstore(oldNumInStore+antiepidemicPurc.getAmount());
        vaccineInforMapper.updateByPrimaryKey(vaccineInfor);*/
        VaccineInfor vaccineInfor=vaccineInforMapper.selectByPrimaryKey(antiepidemicPurc.getVaccineid());
        //库存量为原来的库存量+入库的数量
        vaccineInfor.setNuminstore(oldNumInStore+antiepidemicPurc.getAmount());
        vaccineInforMapper.updateByPrimaryKeySelective(vaccineInfor);
        return 1;
    }

    /**
     * 删除疫苗入库信息
     * @param id 待删除的疫苗入库信息id
     * @return 1成功 0失败
     */
    public int delete(Integer id){
        AntiepidemicPurc antiepidemicPurc= antiepidemicPurcMapper.selectByPrimaryKey(id);
        //删除时需要设置外键为空才能删除成功
        antiepidemicPurc.setVaccineid(null);
        int a=antiepidemicPurcMapper.updateByPrimaryKeySelective(antiepidemicPurc);
        return antiepidemicPurcMapper.deleteByPrimaryKey(id);
    }

    public  AntiepidemicPurc findByInStoreId(Integer instoreid){
        AntiepidemicPurcExample antiepidemicPurcExample=new AntiepidemicPurcExample();
        AntiepidemicPurcExample.Criteria criteria=antiepidemicPurcExample.createCriteria();
        if(instoreid!=null){
            criteria.andInstoreidEqualTo(instoreid);
            return antiepidemicPurcMapper.selectByPrimaryKey(instoreid);
        }
        return null;
    }

    /**
     * 更新疫苗入库信息
     * @param antiepidemicPurc 疫苗入库信息
     * @param oldInStoreNum 原来的入库数量
     * @return 1成功 0失败
     */
    public int update(AntiepidemicPurc antiepidemicPurc,Integer oldInStoreNum){
        //更新疫苗入库信息之前首先要更新疫苗种类信息的库存量
        //获取前后库存量的差值(差值可能为负数,通过"新的入库数量-原来的入库数量"得出)
        int between=antiepidemicPurc.getAmount()-oldInStoreNum;
        VaccineInfor vaccineInfor=vaccineInforMapper.selectByPrimaryKey(antiepidemicPurc.getVaccineid());
        //库存量为现在的库存量+差值
        vaccineInfor.setNuminstore(vaccineInfor.getNuminstore()+between);
        int code=vaccineInforMapper.updateByPrimaryKeySelective(vaccineInfor);
        //如果修改疫苗种类信息的库存量失败就返回不再继续执行
        if(code==0){
            return 0;
        }

        //最后更新修改后的疫苗入库信息
        antiepidemicPurcMapper.updateByPrimaryKey(antiepidemicPurc);
        return 1;
    }
}
